package com.woniuedu.search.serviec.impl;


import com.woniuedu.search.entity.Article;
import com.woniuedu.search.mapper.ArticleMapper;
import com.woniuedu.search.serviec.ArticleService;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
//import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 讲师 服务类
 * </p>
 *
 * @author yuankaiqiang
 * @since 2022-04-19
 */
//@Repository("article")
//    @Service
//    @Controller
//    @MapperScan
//    @Repository
@Service("article")
public class ArticleServiceImpl implements ArticleService {
      @Resource
      private ArticleMapper articleMapper;

    @Override
    public Page<Article> findByAll(String title, Pageable pageable) {
        // 设置高亮,使用默认的highlighter高亮器
        String fields = "title";
        String preTags = "<span style='background-color:yellow'>";
        String postTags = "</span>";
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.matchQuery("title",title));
        // 构建检索条件
        NativeSearchQueryBuilder nativeSearchQueryBuilder =
                new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withPageable(pageable)
                        .withHighlightFields(new HighlightBuilder.Field(fields).preTags(preTags).postTags(postTags))
                ;
        return articleMapper.search(nativeSearchQueryBuilder.build());
    }

    @Override
    public void deleteById(String id) {
        articleMapper.deleteById(id);
    }

    @Override
    public void save(Article article) {
        articleMapper.save(article);
    }
}
